package com.zhihuichongcheng.application.ui.openclass.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseFragment;
import com.zhihuichongcheng.application.bean.openclass.OpenClassBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.openclass.activity.OpenClassDetailActivity;
import com.zhihuichongcheng.application.ui.openclass.adapter.OpenCLassAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/10/29.
 */

public class OpenClassStudyFragment extends BaseFragment {
    private XRecyclerView mRcvStudy;
    private List<OpenClassBean> mListData = new ArrayList<>();
    private OpenCLassAdapter mAdapter = null;
    private String id;
    private int mPageSize = 10;
    private int mPage =1;
    private LinearLayout mLlNoData;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if(bundle!=null&&bundle.containsKey("id"))
        {
            id = bundle.getString("id");
        }
    }

    @Override
    protected void init() {
        super.init();
        initView();
        initData();
    }

    private void initView() {
        mLlNoData = (LinearLayout) mView.findViewById(R.id.ll_nodata);
        mRcvStudy = (XRecyclerView) mView.findViewById(R.id.rcv_healthy);
        mAdapter = new OpenCLassAdapter(mContext);
        mAdapter.setOnItemOnclick(new OnItemOnclick<OpenClassBean>() {
            @Override
            public void onClick(OpenClassBean openClassBean, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("title", openClassBean.getTitle());
                bundle.putString("id", openClassBean.getOpenClassId());
                startActivity(OpenClassDetailActivity.class, bundle);
            }
        });
        mRcvStudy.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvStudy.setAdapter(mAdapter);
    }

    private void initData() {
        loadData(mPage);
        mRcvStudy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                loadData(mPage);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadData(mPage);
            }
        });

    }

    private void loadData(final int page)
    {
        HashMap<String,String> params = new HashMap<>();
        params.put("token",mUser.getToken());
        params.put("openClassCategoryId",id);
        params.put("pageRows",""+mPageSize);
        params.put("curPage",""+page);
        manager.post(NetworkAPI.Openclass_list, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                mListData = GsonUtil.paraeFromStringToList(value,OpenClassBean.class);
                if(page ==1)
                {
                    mAdapter.clear();
                    if(mListData.size()==0)
                    {
                        mLlNoData.setVisibility(View.VISIBLE);
                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mLlNoData.getLayoutParams();
                        params.setMargins(0,(DeviceUtil.getHeight(mContext)-DeviceUtil.dip2px(mContext,50))/10*4-DeviceUtil.dip2px(mContext,75),0,0);
                        mLlNoData.setLayoutParams(params);
                        mRcvStudy.setVisibility(View.GONE);
                    }
                    else
                    {
                        mLlNoData.setVisibility(View.GONE);
                        mRcvStudy.setVisibility(View.VISIBLE);
                    }
                }
                mAdapter.setData(mListData);
                mAdapter.notifyDataSetChanged();
                mRcvStudy.loadMoreComplete();
                mRcvStudy.refreshComplete();
                if(mListData.size()<mPageSize)
                {
                    mRcvStudy.setNoMore(true);
                }
            }

            @Override
            public void onFailure(String value) {
                mLlNoData.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)mLlNoData.getLayoutParams();
                params.setMargins(0,(DeviceUtil.getHeight(mContext)-DeviceUtil.dip2px(mContext,50))/10*4-DeviceUtil.dip2px(mContext,75),0,0);
                mLlNoData.setLayoutParams(params);
                mRcvStudy.setVisibility(View.GONE);
            }
        },params);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_cricle_healthy;
    }
}
